package com.gdk.ksoft.assembly.utils.extensions;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mrper on 2014/12/7.
 * 图形绘制辅助类
 */
public class CanvasUtils {
    /**
     * 获取当前字体的高度
     * @return
     */
    public static float getFontHeight(Paint paint){
        return (paint.descent()-paint.ascent())+2;
    }

    /**
     * 获取需要绘制的字符串
     * @param text
     * @param maxWidth
     * @return
     */
    public static String getNeedDrawString(Paint paint,String text,float maxWidth){
        float textWidth = paint.measureText(text);
        String tempString = "";
        float blankCharWidth = paint.measureText(" ");
        if(textWidth<maxWidth){
            tempString += text;
            int howMany = (int)((maxWidth-textWidth)/blankCharWidth);
            while(howMany>0){
                tempString+=" ";
                howMany--;
            }
            return tempString;
        }else{
            for(int i=0;i<text.length();i++){
                tempString = text.substring(0,i);
                float tempWidth = paint.measureText(tempString);
                if(tempWidth>=maxWidth)
                    return tempString;
            }
        }
        return tempString;
    }

    /**
     * 绘制文本
     * @param canvas
     * @param paint
     * @param text
     * @param x
     * @param y
     * @param maxWidth
     * @return
     */
    public static float drawText(Canvas canvas,Paint paint,String text,float x,float y,float maxWidth){
        if(text == null) text = "";
        float py  = getFontHeight(paint) + y;
        if(maxWidth>0) {
            text = getNeedDrawString(paint, text, maxWidth);
            canvas.drawText(text, x, py, paint);
        }else {
            canvas.drawText(text, x, py, paint);
        }
        return py;
    }

    /**
     * 绘制线条
     * @param canvas
     * @param paint
     * @param x
     * @param y
     * @param w
     * @param h
     */
    public static float drawLine(Canvas canvas,Paint paint,float x,float y,float w,float h){
        float py = y+h+2*h;
        canvas.drawRect(x,2*h+y,x+w,py,paint);
        return py;
    }


    /**
     * 对要绘制的文字折行
     * @param paint
     * @param text
     * @return
     */
    public static List<String> breakText(Paint paint,String text,float maxWidth){
        if(TextUtils.isEmpty(text)) return null;
        List<String> list = null;
        int startPos = 0;//起始切割文字索引
        for(int i=0;i<text.length();i++){
            if(list == null)list = new ArrayList<>();
            float w = paint.measureText(text.substring(startPos,i));
            if(w>=maxWidth){
                list.add(text.substring(startPos, i));//获取当前的字符串
                startPos = i-1;
                continue;
            }
            if(i == text.length()-1) {//如果是最后一行
                list.add(text.substring(startPos, i+1));//获取当前的字符串
            }
        }
        return list;
    }

}
